<template>
	<view>
		<view class="container">
			<view style="margin-top: 10rpx;" v-show="showEmpty==false" v-for="(item,index) in addressList" :key="index">
				<uni-card is-full>
					<view class="address-item">
						<view class="UserName">
							<text>{{item.name}}</text>
							<text>{{item.phoneNumber}}</text>
							<span v-show="item.defaultStatus==1 ? true : false ">默认</span>
						</view>
						<view class="address-detali">
							<view>
								{{item.province}}-{{item.city}}-{{item.region}}-{{item.detailAddress}}
							</view>
							<view @tap="gotoUpdate(item)">
								<text class="iconfont icon-wodedingdan3"></text>
							</view>
						</view>

						<view class="btn-operate">
							<view>
								<switch :checked="item.defaultStatus==1 ? true : false" color="#55aaff"
									@change="changeState(item)" style="transform:scale(0.7)" />
								设置默认
							</view>
							<view @tap="delAddress(item.id)" style="margin-top: 5px;">
								<uni-tag text="删除" type="error" />
							</view>
						</view>
					</view>
				</uni-card>
			</view>

			<view class="bottom">
				<button type="warn" @tap="gotoAdd">新增地址</button>
			</view>
			<view>
				<!-- 提示窗示例 -->
				<uni-popup ref="alertDialog" type="dialog" @touchmove.stop.preven :maskClick="false">
					<uni-popup-dialog type="info" cancelText="关闭" confirmText="同意" title="提示" content="确定删除该地址吗？"
						@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
				</uni-popup>
			</view>
		</view>

		<view v-show="showEmpty">
			<empty text="快去新增地址吧!"></empty>
		</view>
	</view>

</template>

<script>
	import empty from '@/components/empty.vue'
	export default {
		components: {
			empty
		},
		data() {
			return {
				addressList: [],
				addresId: 0,
				showEmpty: false
			}
		},
		onLoad() {
			this.getAddressList()
		},
		onShow() {
			this.getAddressList()
		},
		onHide() {
		
		},
		methods: {
			getAddressList() {
				this.$api.user.getAddress().then(res => {
					if (res.code == 200) {
						this.addressList = res.data
						if (this.addressList.length == 0) {
							this.showEmpty = true
						}else{
								this.showEmpty = false
						}
					}
				})
			},
			delAddress(id) {
				this.addresId = id
				// #ifdef APP-PLUS || H5
				this.$refs.alertDialog.open()
				// #endif

				// #ifdef MP-WEIXIN || QUICKAPP-WEBVIEW || QUICKAPP-WEBVIEW-UNION || QUICKAPP-WEBVIEW-HUAWEI
				uni.showModal({
					title: "确定删除吗？",
					success: (res) => {
						if (res.confirm) {
							this.$api.user.delAddress(this.addresId).then(res => {
								if (res.code == 200) {
									this.getAddressList()
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
				// #endif

			},
			dialogConfirm() {
				this.$api.user.delAddress(this.addresId).then(res => {
					if (res.code == 200) {
						this.getAddressList()
					}
				})
			},
			dialogClose() {
				this.addresId = 0
				this.$refs.alertDialog.close()
			},
			changeState(item) {
				let state = item.defaultStatus == 1 ? 0 : 1
				this.$api.user.updateDefault(item.id, state).then(res => {
					if (res.code == 200) {
						this.getAddressList()
					}
				})
			},
			gotoAdd() {
				uni.navigateTo({
					url: "/pages/view/InsertAddress"
				})
			},
			gotoUpdate(item) {
				uni.navigateTo({
					url: "/pages/view/InsertAddress?address=" + JSON.stringify(item)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding-bottom: 80rpx;

		uni-card {
			margin-top: 10rpx;
		}

		.address-item {
			width: 100%;

			.UserName {
				text:nth-child(1) {
					font-weight: 700;
					font-size: 40rpx;
					color: black;
					padding-right: 10rpx;
				}

				span {
					border: 1px solid orangered;
					margin-left: 10rpx;
					padding: 5rpx;
					color: orangered;
				}
			}

			.address-detali {
				box-sizing: border-box;

				view:nth-child(1) {
					width: 80%;
					height: 100%;
					float: left;
					padding: 10rpx;
				}

				view:nth-child(2) {
					text {
						padding-left: 50rpx;
						font-size: 50rpx;
					}
				}
			}

			.btn-operate {
				clear: both;
				padding-top: 10rpx;
				border-top: 1px solid gainsboro;
				display: flex;
				justify-content: space-between;
			}
		}

		.bottom {
			height: 80rpx;
			position: fixed;
			bottom: 0;
			background-color: white;
			width: 100%;
		}
	}
</style>
